package io.github.qifan777.knowledge.ai.agent.db;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.github.qifan777.knowledge.entity.User;
import io.github.qifan777.knowledge.service.IUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.core.NestedExceptionUtils;

import java.util.List;
import java.util.function.Function;

/**
 * 查询用户表
 * @author tjw
 * @date 2025-04-23
 * @Description
 */
@Slf4j
@AllArgsConstructor
@Configuration
public class UserTools {

    final IUserService userService;
//    public record UserDetailsRequest(String nickname, String phone) {
//    }

    public record UserDetailsRequest(User user) {
    }

    @Bean
    @Description("获取用户数据")
    public Function<UserDetailsRequest, List<User>> getUserDetails() {
        return request -> {
            try {
                LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
                wrapper.eq(StringUtils.isNotEmpty(request.user().getId()),User::getId,request.user().getId());
                wrapper.eq(StringUtils.isNotEmpty(request.user().getNickname()),User::getNickname,request.user().getNickname());
                wrapper.eq(StringUtils.isNotEmpty(request.user().getPhone()),User::getPhone,request.user().getPhone());
                return userService.list(wrapper);
            }
            catch (Exception e) {
                log.error("用户不存在！");
            }
            return null;
        };
    }
}
